package bloothdemo.bloothdemo;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyDeviceAdapter extends BaseAdapter {

    private Context context;

    public void setList(List<BluetoothDevice> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private List<BluetoothDevice> list;

    public MyDeviceAdapter(Context context, List<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = View.inflate(context, R.layout.bluetoothdevice_item_layout, null);
            TextView tvDevice = view.findViewById(R.id.tv_device);
            TextView tvAddress = view.findViewById(R.id.tv_device_address);
            tvDevice.setText(list.get(i).getName());
            tvAddress.setText(list.get(i).getAddress());
        }
        return view;
    }
}
